package com.example.testsqlite;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.ldy.db.DBManager;
import com.ldy.db.Manager;
import com.ldy.db.QueryBuilder;
import com.ldy.db.Where;
import com.ldy.test.Model;
import com.ldy.test.Student;

public class MainActivity extends Activity implements OnClickListener {

	DBManager<Student> dbStudent = new DBManager<Student>(this, Student.class);

	DBManager<Model> dbModel = new DBManager<Model>(this, Model.class);

	private EditText textNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// test1();

		// SqliteHelper helper = new SqliteHelper(this);
		// SQLiteDatabase sqlite = helper.getWritableDatabase();

		findViewById(R.id.insert).setOnClickListener(this);
		findViewById(R.id.query).setOnClickListener(this);
		findViewById(R.id.delete).setOnClickListener(this);
		findViewById(R.id.update).setOnClickListener(this);
		findViewById(R.id.findOne).setOnClickListener(this);

		textNum = (EditText) findViewById(R.id.num);

		Where<Student> where = new Where<Student>();
		Log.i("data", "where:" + where.eq("id", 1).where());

		QueryBuilder<Student> query = new QueryBuilder<Student>(Student.class);
		query.setWhere(where);
		String sql = query.createSQL();
		Log.i("data", "sql:" + sql);
		Student student = dbStudent.queryForFirst(sql, null);
		Log.i("data", "id:" + student.getName());
		
		Manager manager = new Manager(this);
		manager.updateDbName("test_db.db", "aaaa.db");
		manager.copyDb2databases(null,"dd");
		File file = this.getDatabasePath("aaaa.db");
	}

	private void insertStudent() {
		Student stu = new Student();
		stu.setName("AAA");
		stu.setSex("man");
		stu.setDate(new Date());
		dbStudent.insert(stu);
	}

	private void insertModel() {
		Model model = new Model();
		model.setMoney(12);
		model.setName("Hello");
		model.setSex("Boy");
		dbModel.insert(model);
	}

	private void query() {
		Log.i("data", "Student");
		ArrayList<Student> stus = dbStudent.findAll();
		for (Student student : stus) {
			Log.i("data", "id:" + student.getId());
			Log.i("data", "name:" + student.getName());
			Log.i("data", "sex:" + student.getSex());
			Log.i("data", "date" + student.getDate());
		}
	}

	private void queryModel() {
		Log.i("data", "Model");
		ArrayList<Model> models = dbModel.findAll();
		for (Model model : models) {
			Log.i("data", "name:" + model.getName());
			Log.i("data", "sex:" + model.getSex());
			Log.i("data", "sex:" + model.getMoney());
		}
	}

	private void findOne(int id) {
		Student student = dbStudent.findById(id);
		Log.i("data", "name:" + student.getName());
		Log.i("data", "sex:" + student.getSex());
		Log.i("data", "date" + student.getDate());
	}

	/**
	 * by id delete student object
	 * 
	 * @param id
	 */
	private void deleteStudent(int id) {
		dbStudent.deleteById(id);
	}

	private void updateStudent(int id) {
		Student student = dbStudent.findById(id);
		student.setMoney(1024);
		student.setName("Student name");
		Log.i("data", "upate size:" + dbStudent.update(student));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.insert:
				insertStudent();
				insertModel();
				break;
			case R.id.delete:
				int delId = Integer.valueOf(textNum.getText().toString());
				deleteStudent(delId);
				break;
			case R.id.update:
				int updateID = Integer.valueOf(textNum.getText().toString());
				updateStudent(updateID);
				break;
			case R.id.query:
				query();
				queryModel();
				break;
			case R.id.findOne:
				int id = Integer.valueOf(textNum.getText().toString());
				findOne(id);
				break;
		}
	}
}
